<template>
  <n-drawer v-model:show="isDrawer" :width="width" :placement="placement">
    <n-drawer-content :title="title" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="100"
      >
        <n-form-item label="类型" path="type">
          <span>{{ formParams.type === 1 ? '侧边栏菜单' : '' }}</span>
        </n-form-item>
        <n-form-item label="菜单名称" path="menuName">
          <n-input placeholder="请输入菜单名称" v-model:value="formParams.menuName" />
        </n-form-item>
        <n-form-item label="描述" path="description">
          <n-input placeholder="请输入描述" v-model:value="formParams.description" />
        </n-form-item>
        <n-form-item label="菜单ID" path="identity">
          <n-input placeholder="请输入id" v-model:value="formParams.identity" />
        </n-form-item>
        <n-form-item label="Url权限" path="permission">
          <n-input placeholder="请输入Url权限,支持正则" v-model:value="formParams.permission" />
        </n-form-item>
        <n-form-item label="排序" path="sort">
          <n-input-number
            placeholder="请输入排序"
            clearable
            :precision="0"
            v-model:value="formParams.sort"
          />
        </n-form-item>
      </n-form>

      <template #footer>
        <n-space>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
          <n-button @click="handleReset">重置</n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, toRefs } from 'vue';
  import { useMessage } from 'naive-ui';
  import { saveMenu } from '@/api/system/menu';
  import { ResultEnum } from '@/enums/httpEnum';

  const rules = {
    menuName: {
      required: true,
      message: '请输入菜单名称',
      trigger: 'blur',
    },
    identity: {
      required: true,
      message: '请输入id',
      trigger: 'blur',
    },
  };
  export default defineComponent({
    name: 'CreateDrawer',
    components: {},
    props: {
      title: {
        type: String,
        default: '添加顶级菜单',
      },
      pid: {
        type: Number,
        default: 0,
      },
      width: {
        type: Number,
        default: 450,
      },
    },
    emits: ['refreshTreeMenu'],
    setup(props, content) {
      const message = useMessage();
      const formRef: any = ref(null);
      const defaultValueRef = () => ({
        type: 1,
        menuName: '',
        description: '',
        permission: '',
        identity: '',
        sort: 0,
        pid: 0,
      });
      const state = reactive({
        isDrawer: false,
        subLoading: false,
        formParams: defaultValueRef(),
        placement: 'right',
        alertText:
          '该功能主要实时预览各种布局效果，更多完整配置在 projectSetting.ts 中设置，建议在生产环境关闭该布局预览功能。',
      });

      function openDrawer() {
        state.isDrawer = true;
      }

      function closeDrawer() {
        state.isDrawer = false;
      }

      function formSubmit() {
        formRef.value.validate(async (errors) => {
          if (!errors) {
            state.formParams.pid = props.pid;
            const response = await saveMenu(state.formParams);
            const { message: msg, code } = response;
            if (code == ResultEnum.SUCCESS) {
              message.success('添加成功');
            } else {
              message.error(msg);
            }
            // 调用父组件刷新
            content.emit('refreshTreeMenu');
            handleReset();
            closeDrawer();
          } else {
            message.error('请填写完整信息');
          }
        });
      }

      function handleReset() {
        formRef.value.restoreValidation();
        state.formParams = Object.assign(state.formParams, defaultValueRef());
      }

      return {
        ...toRefs(state),
        formRef,
        rules,
        formSubmit,
        handleReset,
        openDrawer,
        closeDrawer,
      };
    },
  });
</script>
